package imitate_LinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-04-14
 * Time: 15:36
 */
public class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }


    public ListNode head;
    public ListNode last;

    //得到双向列表的长度
    public int size(){
        int len = 0;
        ListNode cur = head;
        while(cur != null){
            cur = cur.next;
            len++;
        }
        return len;
    }

    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找双链表是否存在某个节点
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        if(head == null){
            head = node;
            last = node;
            return;
        }
        head.prev = node;
        node.next = head;
        head = node;
    }


    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            addFirst(data);
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }

    public void addIndex(int index, int data){
        if(index < 0 || index > size()){
            throw new IndexOutOfBounds("双向列表index不合法！！");
        }
        if(index == size()) {
            addLast(data);
            return;
        }

        if(index == 0){
            addFirst(data);
            return;
        }

        ListNode cur = head;

        while(index != 0) {
            index -- ;
            cur = cur.next;
        }

        ListNode node = new ListNode(data);

        node.prev  = cur.prev;
        node.next = cur;
        cur.prev.next = node;
        cur.prev = node;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        ListNode cur = head;

        //头结点为要删除的节点
        if(head.val == key) {
            head = head.next;
            //判断是否只有一个头节点
            if(head != null) {
                head.prev = null;
                return;
            }
        }

        //要删除的节点在中间
        while(cur.next != null) {
            if(cur.val == key) {
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                return;
            }
            cur = cur.next;
        }

        //尾节点为要删除的节点
        if(cur.val == key) {
            cur.prev.next = null;
            last = last.prev;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        ListNode cur = head;

        while(cur != null) {
            if(cur.val == key) {
                //判断是否为头结点
                if(cur == head) {
                    head = head.next;
                    if(head != null) {
                        head.prev = null;
                    }else {
                        last = null;
                    }
                } else {
                    cur.prev.next = cur.next;
                    //判断是否为尾节点
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        last = last.prev;
                    }
                }
                cur = cur.next;
            } else {
                cur = cur.next;
            }
        }
    }

    public void clear(){
        ListNode cur = head;

        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }


}
